/**
 * 
 */
package mx.netbrain.bean;

import java.io.Serializable;
import java.util.List;

import javax.faces.application.FacesMessage;
import javax.faces.context.FacesContext;

import mx.netbrain.dao.ProfileDao;
import mx.netbrain.dao.impl.ProfileDaoImpl;
import mx.netbrain.model.Profile;

/**
 * @author julian
 * 
 */
public class ProfileBean implements Serializable {

	/**
	 * 
	 */
	private static final long serialVersionUID = -7265480496717004305L;
	private Profile profile;
	private String descripcion;
	private List<Profile> listProfile;

	/**
	 * Creates a new instance of ProfileBean
	 */
	public ProfileBean() {
	}

	public void prepareProfile(Long id) {
		ProfileDao profileDao = new ProfileDaoImpl();
		profile = profileDao.findById(id);
	}

	public void editProfile() {
		ProfileDao profileDao = new ProfileDaoImpl();
		profileDao.update(profile);
		FacesContext context = FacesContext.getCurrentInstance();
		context.addMessage(null, new FacesMessage(
				"Datos actualizados correctamente!", "Actualizado"));
		profile = new Profile();
	}

	public void saveProfile() {
		ProfileDao profileDao = new ProfileDaoImpl();
		profile.setProfile(descripcion);
		profileDao.save(profile);
		FacesContext context = FacesContext.getCurrentInstance();
		context.addMessage(null, new FacesMessage(
				"El perfil se guardo correctamente!", "Guardado"));
		profile = new Profile();
		descripcion = new String();
	}

	public void delete() {
		ProfileDao profileDao = new ProfileDaoImpl();
		profileDao.delete(profile);
		FacesMessage message = new FacesMessage(FacesMessage.SEVERITY_INFO,
				"Se ha borrado el perfil correctamente!", "Actualizado");
		FacesContext.getCurrentInstance().addMessage(null, message);
		profile = new Profile();
	}

	public Profile getProfile() {
		if (profile == null) {
			profile = new Profile();
		}
		return profile;
	}

	public void setProfile(Profile profile) {
		this.profile = profile;
	}

	public String getDescripcion() {
		return descripcion;
	}

	public void setDescripcion(String descripcion) {
		this.descripcion = descripcion;
	}

	public List<Profile> getListProfile() {
		ProfileDao profileDao = new ProfileDaoImpl();
		listProfile = profileDao.listAll();
		return listProfile;
	}

	public void setListProfile(List<Profile> listProfile) {
		this.listProfile = listProfile;
	}
}
